﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Spider.Mode;

namespace Common.Dao
{
    public class DxwEventDao
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(DxwEventDao));
        //保存分类好的事件
        public static void BatchSaveEvents(List<DxwEvent> news)
        {
            String sql =
                @"INSERT IGNORE training_data(event_type,title,content,block,site_time,stocks,event_id,crawling_time,keywords)  " +
                "values (@EventType,@Title,@Content,@Block,@SiteTime,@Stocks,@EventId,@CrawlingTime,@KeyWords) ON DUPLICATE KEY UPDATE site_time = @SiteTime";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                try
                {
                    int count = conn.InsertMultiple(sql, news);
                    Log.Info("insert successful count "+count);
                }
                catch (Exception e)
                {
                    Log.Error("error list:" + JsonConvert.SerializeObject(news));
                    Log.Error(e);
                }
            }
        }
        
        //保存新闻分词
        public static void BatchSaveEventWords(List<DxwEvent> news)
        {
            String sql =
                @"update training_data set words = @Words where event_id = @EventId";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                var count = conn.InsertMultiple(sql, news);
                Log.Info("insert successful count " + count);
            }
        }

        //保存关键词
        public static void UpdateEventKeywords(List<DxwEvent> news)
        {
            String sql =
                @"update training_data set keywords = @KeyWords where event_id = @EventId";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                var count = conn.InsertMultiple(sql, news);
                Log.Info("insert successful count " + count);
            }
        }
        
        //保存板块分类
        public static void UpdateEventBlock(List<DxwEvent> news)
        {
            String sql =
                @"update training_data set block = @block where event_id = @EventId";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                var count = conn.InsertMultiple(sql, news);
                Log.Info("insert successful count " + count);
            }
        }
       
        //获取全部事件
        public static List<DxwEvent> GetEvents()
        {
            String sql =
                "SELECT keywords,event_id AS EventId,event_type AS EventType,title,content,block,site_time AS SiteTime,stocks,words,crawling_time AS CrawlingTime FROM training_data";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
        
        public static List<string> GetEventBlock()
        {
            String sql =
                "SELECT block FROM training_data";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<string> newsList = conn.Query<string>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
        
        //获取全部事件
        public static List<DxwEvent> GetEventsWithWords()
        {
            String sql =
                "select keywords,event_id as EventId,event_type as EventType,title,content,block,site_time as SiteTime,stocks,words,crawling_time as CrawlingTime from training_data " +
                "where words is not null";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
        
        /// <summary>
        /// 获取有关键词的事件列表
        /// </summary>
        /// <returns></returns>
        public static List<DxwEvent> GetEventsWithKeywords()
        {
            String sql =
                "select keywords,event_id as EventId,event_type as EventType,title,content,block,site_time as SiteTime,stocks,words,crawling_time as CrawlingTime from training_data " +
                "where keywords is not null";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
        
        /// <summary>
        /// 获取没有关键词的事件列表
        /// </summary>
        /// <returns></returns>
        public static List<DxwEvent> GetEventsWithoutKeywords()
        {
            String sql =
                "select event_id as EventId,event_type as EventType,title,content,block,site_time as SiteTime,stocks,words,crawling_time as CrawlingTime from training_data " +
                "WHERE content <>'' AND keywords is null";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return newsList;
                }
            }
        }
        
        //获取全部事件分类
        public static List<string> GetEventBlockDistinct()
        {
            string sql =
                "SELECT DISTINCT block FROM `training_data`";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<string> newsList = conn.Query<string>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
        
        //获取全部事件
        public static List<DxwEvent> GetEventsWithoutWords()
        {
            String sql =
                "SELECT keywords,event_id AS EventId,event_type AS EventType,title,content,block,site_time AS SiteTime,stocks,words,crawling_time AS CrawlingTime FROM training_data " +
                " WHERE content <>'' AND words is null";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }

        //获取全部事件
        public static List<DxwEvent> GetEventsWithContent()
        {
            String sql =
                "select keywords,event_id as EventId,event_type as EventType,title,content,block,site_time as SiteTime,stocks,words,crawling_time as CrawlingTime from training_data " +
                "WHERE content <>''";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }

        //根据板块名称获取事件
        public static List<DxwEvent> GetEvents(string block)
        {
            String sql =
                "select keywords,event_type as EventType,title,content,block,site_time as sitetime,stocks,event_id as eventid,crawling_time as crawlingtime from training_data " +
                "where block = @block order by site_time desc";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<DxwEvent> newsList = conn.Query<DxwEvent>(sql)?.ToList();
                Log.Info("query successful count =" + newsList?.Count());
                if (newsList != null && newsList.Any())
                {
                    return newsList.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
    }
}